package com.ciaojian.core.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.mapper.*;
import com.ciaojian.core.model.StorePerformance;
import com.ciaojian.core.model.Worker;
import com.ciaojian.core.model.vo.StoreRealPerformanceVo;
import com.ciaojian.core.util.BigDecimalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Atlakyn
 */
@Service
public abstract class PerformanceService<M extends BaseMapper<T>, T> extends ServiceImpl<WorkerMapper, Worker> {
    @Resource
    StoreTurnoverMapper storeTurnoverMapper;
    @Resource
    WorkerTurnoverMapper workerTurnoverMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 获取门店和理发师月度目标及完成进度
     *
     * @param storeId  店面 id
     * @param workerId 理发师 id
     * @return map 类型
     * storeTurnover 店面目标
     * storeAchieve 店面完成进度
     * workerTurnover 理发师目标
     * workerAchieve 理发师完成进度
     */
    public Map<String, Object> getPerformance(Integer storeId, Integer workerId, Date startDate, Date endDate) {
        // 店面目标
        BigDecimal storeTurnover = storeTurnoverMapper.getTurnover(storeId, startDate, endDate);
        if (storeTurnover == null) {
            storeTurnover = BigDecimal.ZERO;
        }
        // 店面当月营业额
        BigDecimal storeAchieve = orderMapper.getTurnoverOfStore(storeId, startDate, endDate);

        // 发型师目标
        BigDecimal workerTurnover = workerTurnoverMapper.getTurnover(workerId, startDate, endDate);
        if (workerTurnover == null) {
            workerTurnover = BigDecimal.ZERO;
        }
        // 发型师当月营业额
        BigDecimal workerAchieve = orderMapper.getTurnoverOfWorker(workerId, startDate, endDate);

        HashMap<String, Object> turnoverMap = new HashMap<>(2);
        turnoverMap.put("storeTurnover", storeTurnover.divide(BigDecimalUtil.HUNDRED, 0, RoundingMode.UP));
        turnoverMap.put("storeAchieve", storeAchieve);
        turnoverMap.put("workerTurnover", workerTurnover.divide(BigDecimalUtil.HUNDRED, 0, RoundingMode.UP));
        turnoverMap.put("workerAchieve", workerAchieve);

        return turnoverMap;
    }


}